package com.technology.education.ui.teacher.author

import android.widget.ImageView
import com.angcyo.dsladapter.dslItem
import com.huantansheng.easyphotos.models.album.entity.Photo
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.dslitem.ItemColumnTitleEdit
import com.technology.education.ext.getAppViewModel
import com.technology.education.ext.getItemText
import com.technology.education.ext.loadImage
import com.technology.education.ext.selectPhoto

class AuthorFragment : BaseRecyclerTopBarFragment<AuthorViewModel>() {
    override fun initViews() {
        setTitle("教师资格认证")
    }

    override fun lazyLoadData() {
        setUI()
    }

    private fun setUI() {
        renderAdapter {
            dslItem(ItemColumnTitleEdit(title = "姓名:", tag = "name", hintStr = "请输入您的姓名"))
            dslItem(ItemColumnTitleEdit(title = "身份证号:", tag = "idcard", hintStr = "请输入您的身份证号码")) {
                style.lineHeight = 1
            }

            dslItem(R.layout.item_author_upload) {
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemTag = "front"
                    itemData?.let {
                        if (it is String) {
                            itemHolder.v<ImageView>(R.id.image)?.apply {
                                loadImage(it, this)
                            }
                        }
                    } ?: itemHolder.v<ImageView>(R.id.image)
                        ?.apply {
                            setBackgroundResource(R.mipmap.im_front)
                            scaleType = ImageView.ScaleType.CENTER_CROP
                        }
                    itemHolder.clickItem {
                        selectPhoto(selectedList = arrayListOf()) {
                            if (it.isNotEmpty()) {
                                uploadImage(it, 0)
                            }
                        }
                    }
                }
            }
            dslItem(R.layout.item_author_upload) {
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemTag = "back"
                    itemData?.let {
                        if (it is String) {
                            itemHolder.v<ImageView>(R.id.image)?.apply {
                                loadImage(it, this)
                            }
                        }
                    } ?: itemHolder.v<ImageView>(R.id.image)
                        ?.apply {
                            scaleType = ImageView.ScaleType.CENTER_CROP
                            setBackgroundResource(R.mipmap.im_back)
                        }

                    itemHolder.clickItem {
                        selectPhoto(selectedList = arrayListOf()) {
                            if (it.isNotEmpty()) {
                                uploadImage(it, 1)
                            }
                        }
                    }
                }
            }
            dslItem(R.layout.item_author_upload) {
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemTag = "author"
                    itemData?.let {
                        if (it is String) {
                            itemHolder.v<ImageView>(R.id.image)?.apply {
                                loadImage(it, this)
                            }
                        }
                    } ?: itemHolder.v<ImageView>(R.id.image)
                        ?.apply {
                            scaleType = ImageView.ScaleType.CENTER_CROP
                            setBackgroundResource(R.mipmap.im_author)
                        }

                    itemHolder.clickItem {
                        selectPhoto(selectedList = arrayListOf()) {
                            if (it.isNotEmpty()) {
                                uploadImage(it, 2)
                            }
                        }
                    }
                }
            }
        }
        setBottomButton("提交") {
            mViewModel.name = myAdapter.getItemText("name") ?: ""
            mViewModel.idCard = myAdapter.getItemText("idcard") ?: ""
            mViewModel.commit()
        }
    }

    private fun uploadImage(photos: ArrayList<Photo>, type: Int) {
        showLoading()
        getAppViewModel().upload(photos) {
            when (type) {
                0 -> {
                    mViewModel.frontUrl = it[0]
                    myAdapter.getItemData(2)?.apply {
                        itemData = photos[0].path
                        updateAdapterItem()
                    }
                }
                1 -> {
                    mViewModel.backUrl = it[0]
                    myAdapter.getItemData(3)?.apply {
                        itemData = photos[0].path
                        updateAdapterItem()
                    }
                }
                2 -> {
                    mViewModel.authorUrl = it[0]
                    myAdapter.getItemData(4)?.apply {
                        itemData = photos[0].path
                        updateAdapterItem()
                    }
                }
            }
            dismissLoading()
        }
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            if (it) {
                com.technology.education.ext.getAppViewModel().userInfo.value?.certState = "0"
                onBack()
            }
        }
    }
}